package com.moxc.learn_springai_01.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moxc.learn_springai_01.entity.po.ChatFileMapping;
import com.moxc.learn_springai_01.mapper.ChatFileMappingMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Properties;

/**
 * TODO : 改为AliOSS存储pdf文件
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class AliOssPdfFileRepository implements FileRepository {

    private final ChatFileMappingMapper chatFileMappingMapper;

    @Override
    public boolean save(String chatId, Resource resource) {

        // 保存到本地磁盘
        String filename = resource.getFilename();
        File target = new File(Objects.requireNonNull(filename));
        if (!target.exists()) {
            try {
                Files.copy(resource.getInputStream(), target.toPath());
            } catch (IOException e) {
                log.error("Failed to save PDF resource.", e);
                return false;
            }
        }
        // 保存映射关系到数据库
        ChatFileMapping mapping = new ChatFileMapping();
        mapping.setChatId(chatId);
        mapping.setFileName(filename);
        chatFileMappingMapper.insert(mapping); // 插入数据库

        return true;
    }

    @Override
    public Resource getFile(String chatId) {
        // 使用 LambdaQueryWrapper 通过 chatId 字段查询
        LambdaQueryWrapper<ChatFileMapping> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatFileMapping::getChatId, chatId);

        // 查询对应的映射记录
        ChatFileMapping mapping = chatFileMappingMapper.selectOne(queryWrapper);

        if (mapping != null) {
            return new FileSystemResource(mapping.getFileName());
        }
        return null;
    }
}